<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\manager\BaseManager;
use app\common\manager\CouponManager;
use think\Config;
use think\Exception;

/**
 * 队长中心
 */
class Coupon extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 优惠券列表
     * @return void
     */
    public function list(){
        try {
            $page = $this->request->post('page', 1);
            $type = $this->request->post('type', null);
            $detail = $this->request->post('detail', null);
            if ($page < 1) {
                $page = 1;
            }
            $result = CouponManager::CouponList($type,$detail,$page);
            if ($result['code'] == BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

    }

    /**
     * 领取优惠券
     * @return void
     */
    public function receive()
    {
        try {
            $id = $this->request->post('id');
            $result = CouponManager::ReceiveCoupon($this->user_id,$id);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function userCouponList()
    {
        try {
            $page = $this->request->post('page', 1);
            $type = $this->request->post('type', null);
            $detail = $this->request->post('detail', null);
            $status = $this->request->post('status', null);
            if ($page < 1) {
                $page = 1;
            }
            $result = CouponManager::UserCouponList($this->user_id,$status,$type,$detail,$page);
            if ($result['code'] == BaseManager::$successCode) {
                $this->success($result['message'],$result['data']);
            } else {
                throw new Exception($result['message']);
            }
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}
